/*
 *  photoBoothController.cpp
 *  UseYourHeadApp001
 *
 *  Created by Arne Boon on 5/26/11.
 *  Copyright 2011 __MyCompanyName__. All rights reserved.
 *
 */

#include "photoBoothController.h"

//--------------------------------------------------------------
photoBoothController::photoBoothController() {
	
	cout << "---constructor---photoBoothController---" << endl;
	
}

//--------------------------------------------------------------
photoBoothController::~photoBoothController() {
	
	cout << "---destructor---photoBoothController---" << endl;
	
}


//--------------------------------------------------------------
void photoBoothController::setup() {
	
	cout << "--setup--" << endl;
	
	///------start photobooth
    camWidth = 440;
    camHeight = 480;
    vidGrabber.setVerbose(true);
    vidGrabber.initGrabber(camWidth, camHeight);
    videoMirror = new unsigned char[camWidth*camHeight*3];
    mirrorTexture.allocate(camWidth, camHeight, GL_RGB);
	
    ofBackground(200,200,200);
    totalPixels = GRABBED_VID_WIDTH * GRABBED_VID_HEIGHT * 4;
    totalPixelsTop = GRABBED_VID_WIDTH * GRABBED_VID_TOP * 4;
	
    top.allocate(GRABBED_VID_WIDTH, GRABBED_VID_TOP, GL_RGBA);
    bottom.allocate(GRABBED_VID_WIDTH, GRABBED_VID_HEIGHT, GL_RGBA);
    snapShotTaken = false;
    offsetMouth = 0;
    mouthOpen = false;
    animation = false;
    setScale();
	
}

//--------------------------------------------------------------
void photoBoothController::update() {
	
	mirrorVideo();
    setHead();
    moveMouth();
	
}

//--------------------------------------------------------------
void photoBoothController::draw() {
	
	ofSetColor(255,255,255);
    //snapShot.draw(0, 0);
    if(animation == false){
		mirrorTexture.draw(0, 0);
		ofLine(0, GRABBED_VID_TOP, GRABBED_VID_WIDTH, GRABBED_VID_TOP);
    }
    
	if(animation == true){
		top.draw(200, 200, 50, 50);
		bottom.draw(200, 200 + offsetMouth, 50, 50);
    }
	
}

//--------------------------------------------------------------
void photoBoothController::keyPressed(int key) {
	
	if (key == ' ' || key == 'f') {
        snapShot.grabScreen(0, 0, GRABBED_VID_WIDTH, GRABBED_VID_HEIGHT);
        snapShotTaken = true;
        animation = true;
    }
	
}


//--------------------------------------------------------------
void photoBoothController::setScale() {
	
    headX = GRABBED_VID_WIDTH / 6;
    headTopY = GRABBED_VID_TOP / 6;
    headBottomY = GRABBED_VID_BOTTOM / 6;
}


//--------------------------------------------------------------
void photoBoothController::setHead()
{
    if (snapShotTaken == true)
    {
        snapShot.setImageType(OF_IMAGE_COLOR_ALPHA);
        snapShotPixels = snapShot.getPixels();
        
		for (int i = 0; i < totalPixels; i += 4)
        {
			
            if (i >= GRABBED_VID_WIDTH * GRABBED_VID_BOTTOM * 4)
            {
                //  if(snapShotPixels[i]>=45&&snapShotPixels[i+1]<=200&&snapShotPixels[i+2]>=45){
                if(snapShotPixels[i]>35&&snapShotPixels[i]<80&&
				   snapShotPixels[i+1]>110&&snapShotPixels[i+1]<170&&
				   snapShotPixels[i+2]>50&&snapShotPixels[i+2]<99) 
				{
					
                    dataPixels[i - totalPixelsTop] = 255;
					dataPixels[i + 1 - totalPixelsTop] = 255;
					dataPixels[i + 2 - totalPixelsTop] = 255;
					dataPixels[i + 3 - totalPixelsTop] = 0;
				
				}
				else
				{
					dataPixels[i - totalPixelsTop] = snapShotPixels[i];
					dataPixels[i + 1 - totalPixelsTop] = snapShotPixels[i + 1];
					dataPixels[i + 2 - totalPixelsTop] = snapShotPixels[i + 2];
					dataPixels[i + 3 - totalPixelsTop] = 0;
				}
			}
		}
	
		top.loadData(snapShotPixels, GRABBED_VID_WIDTH, GRABBED_VID_TOP, GL_RGBA);
		bottom.loadData(dataPixels, GRABBED_VID_WIDTH, GRABBED_VID_BOTTOM, GL_RGBA);
	
		snapShotTaken = false;
	}
}

//--------------------------------------------------------------
void photoBoothController::moveMouth() {
    if (mouthOpen == false) {
        offsetMouth += 1.5;
        if (offsetMouth >= 10) {
            mouthOpen = true;
			
        }
    }
    else {
        offsetMouth -= 1.5;
        if (offsetMouth <= 0) {
			
            mouthOpen = false;
        }
    }
}


//--------------------------------------------------------------
void photoBoothController::mirrorVideo(){
    vidGrabber.grabFrame();
	if (vidGrabber.isFrameNew()) {
		unsigned char * pixels = vidGrabber.getPixels();
		for (int i = 0; i < camHeight; i++) {
			for (int j = 0; j < camWidth*3; j+=3) {
				// pixel number
				int pix1 = (i*camWidth*3) + j;
				int pix2 = (i*camWidth*3) + (j+1);
				int pix3 = (i*camWidth*3) + (j+2);
				// mirror pixel number
				int mir1 = (i*camWidth*3)+1 * (camWidth*3 - j-3);
				int mir2 = (i*camWidth*3)+1 * (camWidth*3 - j-2);
				int mir3 = (i*camWidth*3)+1 * (camWidth*3 - j-1);
				// swap pixels
				videoMirror[pix1] = pixels[mir1];
				videoMirror[pix2] = pixels[mir2];
				videoMirror[pix3] = pixels[mir3];
			}
		}
		mirrorTexture.loadData(videoMirror, camWidth, camHeight, GL_RGB);
	}
}